<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'" :disabled="disabled">

      <el-row>
        <el-col :span="12">
          <el-form-item label="导游" prop="guideName">
            <el-input v-model="dataForm.guideName" placeholder="导游"></el-input>
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="评论用户" prop="userName">
            <el-input v-model="dataForm.userName" placeholder="评论用户"></el-input>
          </el-form-item>

        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="评论类型" prop="type">
            <el-select v-model="dataForm.type">
              <el-option v-for="item, index in commentList" :key="index" :value="item.value" :label="item.name">

              </el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="评论内容" prop="comment">
            <el-input v-model="dataForm.comment" placeholder="评论内容"></el-input>
          </el-form-item>

        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="评论分数" prop="score">
            <el-input v-model="dataForm.score" placeholder="评论分数" type="number"></el-input>
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="路线名称" prop="routeName">
            <el-input v-model="dataForm.routeName" placeholder="路线"></el-input>
          </el-form-item>

        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否有图片" prop="isImg">
            <el-radio v-model="dataForm.isImg" :label="item.value" v-for="item, index in radioOptions" :key="index">{{
              item.name }}</el-radio>
          </el-form-item>

        </el-col>
        <el-col :span="12">


        </el-col>
      </el-row>
      <!-- <el-form-item label="创建人" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="updater">
        <el-input v-model="dataForm.updater" placeholder="修改人"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateDate">
        <el-input v-model="dataForm.updateDate" placeholder="修改时间"></el-input>
      </el-form-item> -->
    </el-form>
    <template slot="footer" v-if="!disabled">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      commentList: [
        {
          name: '中差评',
          value: '1'
        },
        {
          name: '好评',
          value: '2'
        },
      ],
      radioOptions: [
        {
          name: '是',
          value: '1'
        },
        {
          name: '否',
          value: '0'
        },
      ],
      visible: false,
      dataForm: {
        routeName: '',
        guideName: '',
        userName: '',
        id: '',
        guideId: '123',
        userId: '123',
        type: '',
        comment: '',
        score: '',
        routeId: '123',
        routeName: '',
        isImg: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  props: { disabled: Boolean },

  computed: {
    dataRule() {
      return {
        guideId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        comment: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        score: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        routeId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        routeName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isImg: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        creator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updater: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/scenic/guidecomment/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/scenic/guidecomment/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
